package View;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ImagePanel extends JPanel implements ActionListener 
{

    private BufferedImage img;
    private JPanel iPanel, middlePanel;
    private JButton newBtn, loadBtn, helpBtn, quitBtn;
    private Font defFont = new Font("Dialog", Font.PLAIN, 20);
    private URL helpURL;
    private static boolean DEBUG = false;
    private JEditorPane htmlPane;

    public ImagePanel(int width, int height) 
    {
        setLayout(new FlowLayout());
        try {
            img = ImageIO.read(new File("C:/Users/Harsh/Documents/Work-Files/2011-2012/Software Engineering/SoftEng-GroupProject/Code/INCREMENT2/trunk/UppersAndDowners(source code)/src/View/newboard.jpeg"));
        } catch (Exception e) {
        }
        repaint();

        addComponents();

    }//End of image panel

    @Override
    public void paintComponent(Graphics g) {
        this.paintComponents(g);
        g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), null);
    }

    private void addComponents() {

        GridLayout layout = new GridLayout(5, 0);
        layout.setVgap(20);
        middlePanel = new JPanel(layout);

        newBtn = new JButton("NEW GAME");
        newBtn.setBackground(Color.red);
        newBtn.setForeground(Color.blue);
        newBtn.setFont(defFont);
        newBtn.addActionListener(this);

        loadBtn = new JButton("LOAD GAME");
        loadBtn.setBackground(Color.blue);
        loadBtn.setForeground(Color.red);
        loadBtn.setFont(defFont);
        loadBtn.addActionListener(this);

        helpBtn = new JButton("HELP");
        helpBtn.setBackground(Color.red);
        helpBtn.setForeground(Color.blue);
        helpBtn.setFont(defFont);
        helpBtn.addActionListener(this);

        quitBtn = new JButton("QUIT");
        quitBtn.setBackground(Color.blue);
        quitBtn.setForeground(Color.red);
        quitBtn.setFont(defFont);
        quitBtn.addActionListener(this);

        middlePanel.add(newBtn);
        middlePanel.add(loadBtn);
        middlePanel.add(helpBtn);
        middlePanel.add(quitBtn);
        middlePanel.setOpaque(false);

        middlePanel.setBorder(BorderFactory.createEmptyBorder(70, 0, 0, 0));
        add(middlePanel);

        //  throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("NEW GAME")) {
            JOptionPane.showMessageDialog(null, "New Game");
        } else if (e.getActionCommand().equals("LOAD GAME")) {
            JOptionPane.showMessageDialog(null, "");
        } else if (e.getActionCommand().equals("QUIT")) {
            int answer = JOptionPane.showConfirmDialog(null, "Are you sure to quit game?", "Warning", JOptionPane.YES_OPTION, JOptionPane.NO_OPTION);
            if (answer == JOptionPane.YES_OPTION) {
                System.exit(0);
            }

        } else if (e.getActionCommand().equals("HELP")) {

            JFrame newFrame = new JFrame("New Window");

            htmlPane = new JEditorPane();
            htmlPane.setEditable(false);
            System.out.println("About to call help method");
            gameHelp();
            JScrollPane htmlView = new JScrollPane(htmlPane);

            newFrame.setPreferredSize(new Dimension(1000, 900));
            newFrame.setLocationRelativeTo(null);
            newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            newFrame.add(htmlView);
            newFrame.setVisible(true);

        }

    }//End method actionPerformed

    private void gameHelp() {
        String s = "BBC.html";
        helpURL = getClass().getResource(s);
        System.out.println("This is the URL " + helpURL);
        if (helpURL == null) {
            JOptionPane.showMessageDialog(null, "Couldn't open help file: " + s);
        } else if (DEBUG) 
        {
            JOptionPane.showMessageDialog(null, "Help URL is " + helpURL);
        }

        displayURL(helpURL);

    }//End gameHelp

    private void displayURL(URL url) {
        System.out.println("I'm inside display url method");

        try {
            if (url != null) {
                System.out.println("my url is not null");
                htmlPane.setPage(url);
                System.out.println("setting url pane");

            } else { //null url
                htmlPane.setText("File Not Found");
                if (DEBUG) {
                    System.out.println("Attempted to display a null URL.");
                }
            }
        } catch (IOException e) {
            System.err.println("Attempted to read a bad URL: " + url);
        }
    }
}//End of class ImagePanel